﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SecurityApi.DataAccess
{
    public class MembershipRepository : BaseRepository
    {
        public CustomMembership GetMembershipById(int userId)
        {
            var query = (from u in _context.CustomMemberships where u.UserId == userId select u).SingleOrDefault();
            return query;
        }

        public bool AddMembership(CustomMembership membership)
        {
            bool bSuccess = false;
            try
            {
                _context.CustomMemberships.Add(membership);
                _context.SaveChanges();
                bSuccess = true;
            }
            catch (Exception ex)
            {
                bSuccess = false;
            }

            return bSuccess;
        }

        public bool RemoveUser(CustomMembership membership)
        {
            bool bResult = true;

            try
            {
                CustomMembership user = _context.CustomMemberships.Where(p => p.UserId == membership.UserId).SingleOrDefault();
                if (null != user)
                {
                    _context.CustomMemberships.Remove(user);
                    _context.SaveChanges();
                }
                else
                {
                    bResult = false;
                }
            }
            catch (Exception ex)
            {
                bResult = false;
            }

            return bResult;
        }
   
    }
}
